//
//  KKTabbarItem.swift
//  SinaWeiBo
//
//  Created by King.Kong. on 16/5/28.
//  Copyright © 2016年 King.Kong. All rights reserved.
//

import UIKit

class KKTabbarItem: UITabBarItem {

    //重写属性
    override var badgeValue: String? {
        didSet {
            //TODO:通过KVC拿到
            let target = valueForKey("_target") as! KKMainViewController
            for subview in target.tabBar.subviews{
                if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                    for v in subview.subviews {
                        if v.isKindOfClass(NSClassFromString("_UIBadgeView")!) {
                            for bgView in v.subviews {
                                if bgView.isKindOfClass(NSClassFromString("_UIBadgeBackground")!) {
                                    print(bgView,"终于找到你,还好没放弃")
                                    
                                    
                                    let image = UIImage(named: "main_badge")
                                    //调用 _UIBadgeBackground类型的对象的KVC方法 简介设置值
                                    bgView.setValue(image, forKey: "_image")
                                }
                            }
                        }
                    }
                }
            }
        }
    }

}
